//
//  HUAlert.m
//  蓝牙
//
//  Created by Mac on 16/2/29.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "HUAlert.h"

@interface HUAlert ()
{
    MBProgressHUD* hud;
}
@end

@implementation HUAlert



+ (void)showMessage:(NSString *)message{
    static BOOL isRuning = NO;              //限定提示框只出现一个
    if (!isRuning) {
        isRuning = YES;
    }else {
        return;
    }
    [HUAlert cancelPreviousPerformRequestsWithTarget:self selector:@selector(showMessage:) object:message];
    UIWindow * window            = [UIApplication sharedApplication].keyWindow;
    UIView *showview             = [[UIView alloc]init];
    showview.backgroundColor     = [UIColor blackColor];

    showview.alpha               = 0.8f;
    showview.layer.cornerRadius  = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];

    UILabel *label               = [[UILabel alloc]init];

    CGRect rect                  = [message boundingRectWithSize:CGSizeMake(320, 1000)
                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}
                                                         context:nil];
    label.frame                  = CGRectMake(0, 0, rect.size.width + 30, rect.size.height + 20);
    label.text                   = message;
    label.textColor              = [UIColor whiteColor];
    label.textAlignment          = 1;
    label.backgroundColor        = [UIColor clearColor];
    label.font                   = [UIFont boldSystemFontOfSize:14];

    showview.frame               = CGRectMake((SCREEN.width - rect.size.width - 20)/2, SCREEN.height * 0.618,rect.size.width + 30,rect.size.height + 20);
    [showview addSubview:label];
    [UIView animateWithDuration:1 animations:^{
        showview.center = CGPointMake(showview.center.x, showview.center.y + 30);
    }];
    [UIView animateWithDuration:1.5 animations:^{               //动画时间
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
        isRuning = NO;
    }];
}

+ (void)showHubWithTitle:(NSString *)title Detaile:(NSString *)detail {
    UIWindow * window    = [UIApplication sharedApplication].keyWindow;
//    MBProgressHUD* hub   = [MBProgressHUD HUDForView:window];
    MBProgressHUD* hub = [MBProgressHUD shareHUB];
    hub.labelText        = title;
    hub.detailsLabelText = detail;
    [window addSubview:hub];
    [hub show:YES];
}
+ (void)hideHub {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
//    MBProgressHUD* hub = [(MBProgressHUD *)window viewWithTag:10022]
    [MBProgressHUD hideAllHUDsForView:window animated:YES];
}
+ (void)setTitle:(NSString *)title detail:(NSString *)detail afterDely:(NSTimeInterval)time {
    MBProgressHUD* hub = [MBProgressHUD shareHUB];
    [hub setLabelText:title];
    [hub setDetailsLabelText:detail];
    [hub hide:YES afterDelay:time];
}
@end
